/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telemetria.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.telemetria.HibernateUtil;
import com.telemetria.entity.Pessoa;

/**
 *
 * @author ARGLAS
 */
public class PessoaDaoImpl implements PessoaDao{

    public void save(Pessoa pessoa) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.save(pessoa);
        t.commit();
        session.close();
    }

    public void delete(Pessoa pessoa) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.delete(pessoa);
        t.commit();
        session.close();
    }
    
    public void update(Pessoa pessoa) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.update(pessoa);
        t.commit();
    }

    public List<Pessoa> find(Pessoa pessoa) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        List<Pessoa> lista = session.createQuery("from Pessoa").list();
        t.commit();
        session.close();
        return lista;
    }

    public Pessoa findByPK(long pk) {
    	
    	Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query = session.createQuery("from Pessoa p where p.id = :pk");
        query.setParameter("pk", pk);
        Pessoa pessoa =  (Pessoa) query.uniqueResult();
        t.commit();        
        session.close();
        return pessoa;
    }
    
}
